﻿using NDF.Utilities;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography;
using NDF.Security.Cryptography;

namespace NDF.Tests.Framework.Security
{
    [TestClass]
    public class SecurityTest1
    {
        string text = "ChenJianwei";

        [TestMethod]
        public void CngAlgorithmTest()
        {
            Console.WriteLine(CngAlgorithm.MD5.Algorithm);
        }


        [TestMethod]
        public void HashAlgorithmTest()
        {
            byte[] bytes = text.ToBytes();

            KeyedHashAlgorithm keyHash = KeyedHashAlgorithm.Create();
            MD5 md5 = MD5.Create();
            RIPEMD160 ripe = RIPEMD160.Create();
            SHA1 sha1 = SHA1.Create();
            SHA256 sha256 = SHA256.Create();
            SHA384 sha384 = SHA384.Create();
            SHA512 sha512 = SHA512.Create();

            keyHash.Key = "cjw".ToBytes();

            string
                text1 = BitConverter.ToString(keyHash.ComputeHash(bytes)).Replace("-", ""),
                text2 = BitConverter.ToString(md5.ComputeHash(bytes)).Replace("-", ""),
                text3 = BitConverter.ToString(ripe.ComputeHash(bytes)).Replace("-", ""),
                text4 = BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", ""),
                text5 = BitConverter.ToString(sha256.ComputeHash(bytes)).Replace("-", ""),
                text6 = BitConverter.ToString(sha384.ComputeHash(bytes)).Replace("-", ""),
                text7 = BitConverter.ToString(sha512.ComputeHash(bytes)).Replace("-", "");

            Console.WriteLine("{0}: {1}", text1.Length, text1);
            Console.WriteLine(keyHash.GetType());

            Console.WriteLine("{0}: {1}", text2.Length, text2);
            Console.WriteLine(md5.GetType());

            Console.WriteLine("{0}: {1}", text3.Length, text3);
            Console.WriteLine(ripe.GetType());

            Console.WriteLine("{0}: {1}", text4.Length, text4);
            Console.WriteLine(sha1.GetType());

            Console.WriteLine("{0}: {1}", text5.Length, text5);
            Console.WriteLine(sha256.GetType());

            Console.WriteLine("{0}: {1}", text6.Length, text6);
            Console.WriteLine(sha384.GetType());

            Console.WriteLine("{0}: {1}", text7.Length, text7);
            Console.WriteLine(sha512.GetType());
        }


        [TestMethod]
        public void HashAlgorithmsTest()
        {
            byte[] bytes = text.ToBytes();
            byte[] key = "sbmy".ToBytes();

            Console.WriteLine(HashAlgorithms.MD5(bytes).ToString());
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.MD5(bytes, key)));
            Console.WriteLine();
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.RIPEMD160(bytes)));
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.RIPEMD160(bytes, key)));
            Console.WriteLine();
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA1(bytes)));
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA1(bytes, key)));
            Console.WriteLine();
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA256(bytes)));
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA256(bytes, key)));
            Console.WriteLine();
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA384(bytes)));
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA384(bytes, key)));
            Console.WriteLine();
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA512(bytes)));
            Console.WriteLine(BitConverter.ToString(HashAlgorithms.SHA512(bytes, key)));
        }
        


        [TestMethod]
        public void CryptoConfigTest()
        {
            var bytes1 = System.Text.Encoding.Default.GetBytes(text);
            var bytes2 = Convert.FromBase64String(text);

            //Console.WriteLine(BitConverter.ToString(bytes1));
            //Console.WriteLine(BitConverter.ToString(bytes2));

            Console.WriteLine(Convert.FromBase64String("58-9D-98-C5-9E-B0-5F-3E-C7-0B-55-EF-35-0A-2D-7F"));

            //var type1 = typeof(byte[]);
            //var type2 = typeof(KeyedHashAlgorithm).GetProperty("Key").PropertyType;
            //Console.WriteLine(type1 == type2);
        }
    }
}
